package com.bootdo.market.common;

import com.alibaba.fastjson.JSON;
import com.bootdo.market.domain.ProductAdvertisementDO;
import com.bootdo.market.domain.ProductAdvpositionDO;
import com.bootdo.market.domain.ProductArticleDO;
import com.bootdo.market.domain.ProductCatalogDO;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class HistoryTodayRedis {

    @Resource(name = "redisHistory")
    private RedisTemplate redisTemplate;

    public void saveProductArticleRedis(ProductArticleDO productArticle) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", productArticle.getId());
        map.put("articleTitle", productArticle.getArticletitle() == null ? "" : productArticle.getArticletitle());
        map.put("articleContent", productArticle.getArticlecontenthidden() == null ? "" : productArticle.getArticlecontenthidden());
        map.put("tagId", productArticle.getTagid() == null ? "" : productArticle.getTagid());
        map.put("articleCoverUrl", productArticle.getArticlecoverurl() == null ? "" : productArticle.getArticlecoverurl());
        map.put("articleHistoryDate", productArticle.getArticlehistorydate() == null ? "" : productArticle.getArticlehistorydate());
        map.put("createTime", productArticle.getCreatetime() == null ? "" : productArticle.getCreatetime());
        if (redisTemplate.opsForZSet().zCard("product_article_" + productArticle.getCatalogid()) > 0) {
            redisTemplate.opsForZSet().removeRangeByScore("product_article_" + productArticle.getCatalogid(), Double.valueOf(productArticle.getArticlesort()), Double.valueOf(productArticle.getArticlesort()));
        }
        redisTemplate.opsForZSet().add("product_article_" + productArticle.getCatalogid(), JSON.toJSONString(map), Double.valueOf(productArticle.getArticlesort()));
        Map<String, Object> detailmap = new HashMap<>();
        detailmap.put("title", productArticle.getArticletitle() == null ? "" : productArticle.getArticletitle());
        detailmap.put("content", productArticle.getArticlecontenthidden() == null ? "" : productArticle.getArticlecontenthidden());
        detailmap.put("create_time", productArticle.getCreatetime() == null ? "" : productArticle.getCreatetime());
        redisTemplate.delete("detail_" + productArticle.getId());
        redisTemplate.opsForValue().set("detail_" + productArticle.getId(), JSON.toJSONString(detailmap));
    }

    public void deleteProductArticleRedis(ProductArticleDO productArticle) {
        if (redisTemplate.opsForZSet().zCard("product_article_" + productArticle.getCatalogid()) > 0) {
            redisTemplate.opsForZSet().removeRangeByScore("product_article_" + productArticle.getCatalogid(), Double.valueOf(productArticle.getArticlesort()), Double.valueOf(productArticle.getArticlesort()));
        }
        redisTemplate.delete("detail_" + productArticle.getId());
    }

    public void saveAdvpositionConfigRedis(ProductAdvpositionDO productAdvposition) {


        Map<String, Object> detailmap = new HashMap<>();
        detailmap.put("productId", productAdvposition.getProductid());
        detailmap.put("advPositionWide", productAdvposition.getAdvpositionwide() == null ? "" : productAdvposition.getAdvpositionwide());
        detailmap.put("advPositionHeight", productAdvposition.getAdvpositionheight() == null ? "" : productAdvposition.getAdvpositionheight());
        detailmap.put("advPositionName", productAdvposition.getAdvpositionname() == null ? "" : productAdvposition.getAdvpositionname());
        detailmap.put("advPositionType", productAdvposition.getAdvpositiontype() == null ? "" : productAdvposition.getAdvpositiontype());
        detailmap.put("advInterval", productAdvposition.getAdvInterval() == null ? "" : productAdvposition.getAdvInterval());
        redisTemplate.opsForHash().put(productAdvposition.getProductid() + "_" + productAdvposition.getAdvpositionid() + "_ad", "advposition_config_" + productAdvposition.getAdvpositionid(), JSON.toJSONString(detailmap));
        /*        redisTemplate.delete("advposition_config_" + productAdvposition.getAdvpositionid());
        redisTemplate.opsForValue().set("advposition_config_" + productAdvposition.getAdvpositionid(), JSON.toJSONString(detailmap));*/
    /*    if (redisTemplate.opsForZSet().zCard(productAdvposition.getProductid() + "_" + productAdvposition.getAdvpositionid() + "_adv") > 0) {
            redisTemplate.opsForZSet().removeRangeByScore(productAdvposition.getProductid() + "_" + productAdvposition.getAdvpositionid() + "_adv", Double.valueOf(0), Double.valueOf(0));
        }
        redisTemplate.opsForZSet().add(productAdvposition.getProductid() + "_" + productAdvposition.getAdvpositionid() + "_adv", JSON.toJSONString(detailmap), Double.valueOf(0));*/

    }

    public void deleteAdvpositionConfigRedis(ProductAdvpositionDO productAdvposition) {
        redisTemplate.opsForHash().delete(productAdvposition.getProductid() + "_" + productAdvposition.getAdvpositionid() + "_ad", "advposition_config_" + productAdvposition.getAdvpositionid());
/*
        redisTemplate.delete("advposition_config_" + productAdvposition.getAdvpositionid());
*/
      /*  if (redisTemplate.opsForZSet().zCard(productAdvposition.getProductid() + "_" + productAdvposition.getAdvpositionid() + "_adv") > 0) {
            redisTemplate.opsForZSet().removeRangeByScore(productAdvposition.getProductid() + "_" + productAdvposition.getAdvpositionid() + "_adv", Double.valueOf(0), Double.valueOf(0));
        }*/
    }


    public void saveProductAdvertisementRedis(List<Map<String, Object>> mapList, ProductAdvertisementDO aaaa) {
        redisTemplate.opsForHash().put(aaaa.getProductid() + "_" + aaaa.getAdvpositionid() + "_ad", "chid_" + aaaa.getChannelid(), JSON.toJSONString(mapList));

        /*Map<String, Object> map = new HashMap<>();
        map.put("id", productAdvertisementDO.getId() == null ? "" : productAdvertisementDO.getId());
        map.put("productId", productAdvertisementDO.getProductid() == null ? "" : productAdvertisementDO.getProductid());
        map.put("advertiserName", productAdvertisementDO.getAdvertisername() == null ? "" : productAdvertisementDO.getAdvertisername());
        map.put("advEvent", productAdvertisementDO.getAdvevent() == null ? "" : productAdvertisementDO.getAdvevent());
        map.put("advDes", productAdvertisementDO.getAdvdes() == null ? "" : productAdvertisementDO.getAdvdes());
        map.put("advType", productAdvertisementDO.getAdvtype() == null ? "" : productAdvertisementDO.getAdvtype());
        map.put("advName", productAdvertisementDO.getAdvname() == null ? "" : productAdvertisementDO.getAdvname());
        map.put("advImgUrl", productAdvertisementDO.getAdvimgurl() == null ? "" : productAdvertisementDO.getAdvimgurl());
        map.put("advUrl", productAdvertisementDO.getAdvurl() == null ? "" : productAdvertisementDO.getAdvurl());
        map.put("advJs", productAdvertisementDO.getAdvjs() == null ? "" : productAdvertisementDO.getAdvjs());
        map.put("advJsPathUrl", productAdvertisementDO.getAdvJsPathUrl() == null ? "" : productAdvertisementDO.getAdvJsPathUrl());
*//*
        map.put("advNum", productAdvertisementDO.getAdvnum() == null ? "" : productAdvertisementDO.getAdvnum());
*//*
        map.put("isDirectional", productAdvertisementDO.getIsdirectional() == null ? "" : productAdvertisementDO.getIsdirectional());
        map.put("area", productAdvertisementDO.getEdareas() == null ? "" : productAdvertisementDO.getEdareas());
        map.put("advContent", productAdvertisementDO.getAdvcontent() == null ? "" : productAdvertisementDO.getAdvcontent());
        map.put("advDescription", productAdvertisementDO.getAdvDescription() == null ? "" : productAdvertisementDO.getAdvDescription());
        map.put("settlementPosition", productAdvertisementDO.getSettlementposition() == null ? "" : productAdvertisementDO.getSettlementposition());
        map.put("settlementW", productAdvertisementDO.getSettlementw() == null ? "" : productAdvertisementDO.getSettlementw());
        map.put("settlementH", productAdvertisementDO.getSettlementh() == null ? "" : productAdvertisementDO.getSettlementh());
        if (redisTemplate.opsForZSet().zCard(productAdvertisementDO.getProductid() + "_" + +productAdvertisementDO.getAdvpositionid() + "_adv") > 0) {
            redisTemplate.opsForZSet().removeRangeByScore(productAdvertisementDO.getProductid() + "_" + +productAdvertisementDO.getAdvpositionid() + "_adv", Double.valueOf(productAdvertisementDO.getAdvsort()), Double.valueOf(productAdvertisementDO.getAdvsort()));
        }
        redisTemplate.opsForZSet().add(productAdvertisementDO.getProductid() + "_" + +productAdvertisementDO.getAdvpositionid() + "_adv", JSON.toJSONString(map), Double.valueOf(productAdvertisementDO.getAdvsort()));*/
    }


    public void deleteProductAdvertisementRedis(ProductAdvertisementDO productAdvertisementDO) {
        redisTemplate.opsForHash().delete(productAdvertisementDO.getProductid() + "_" + productAdvertisementDO.getAdvpositionid() + "_ad", "chid_" + productAdvertisementDO.getChannelid());

        /*if (redisTemplate.opsForZSet().zCard(productAdvertisementDO.getProductid() + "_" + +productAdvertisementDO.getAdvpositionid() + "_adv") > 0) {
            redisTemplate.opsForZSet().removeRangeByScore(productAdvertisementDO.getProductid() + "_" + +productAdvertisementDO.getAdvpositionid() + "_adv", Double.valueOf(productAdvertisementDO.getAdvsort()), Double.valueOf(productAdvertisementDO.getAdvsort()));
        }*/
    }

    public void saveProductCatalogRedis(ProductCatalogDO productCatalogDO) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", productCatalogDO.getId() == null ? "" : productCatalogDO.getId());
        map.put("catalogType", productCatalogDO.getCatalogtype() == null ? "" : productCatalogDO.getCatalogtype());
        map.put("catalogName", productCatalogDO.getCatalogname() == null ? "" : productCatalogDO.getCatalogname());
        map.put("catalogUrl", productCatalogDO.getCatalogurl() == null ? "" : productCatalogDO.getCatalogurl());
        map.put("catalogSort", productCatalogDO.getCatalogsort() == null ? "" : productCatalogDO.getCatalogsort());
        if (redisTemplate.opsForZSet().zCard(productCatalogDO.getProductid() + "_catalog") > 0) {
            redisTemplate.opsForZSet().removeRangeByScore(productCatalogDO.getProductid() + "_catalog", Double.valueOf(productCatalogDO.getCatalogsort()), Double.valueOf(productCatalogDO.getCatalogsort()));
        }
        redisTemplate.opsForZSet().add(productCatalogDO.getProductid() + "_catalog", JSON.toJSONString(map), Double.valueOf(productCatalogDO.getCatalogsort()));
    }

    public void deleteProductCatalogRedis(ProductCatalogDO productCatalogDO) {
        if (redisTemplate.opsForZSet().zCard(productCatalogDO.getProductid() + "_catalog") > 0) {
            redisTemplate.opsForZSet().removeRangeByScore(productCatalogDO.getProductid() + "_catalog", Double.valueOf(productCatalogDO.getCatalogsort()), Double.valueOf(productCatalogDO.getCatalogsort()));
        }
    }

    public void saveStickypostsRedis(ProductArticleDO productArticle) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", productArticle.getId());
        map.put("articleTitle", productArticle.getArticletitle() == null ? "" : productArticle.getArticletitle());
        map.put("articleContent", productArticle.getArticlecontenthidden() == null ? "" : productArticle.getArticlecontenthidden());
        map.put("tagId", productArticle.getTagid() == null ? "" : productArticle.getTagid());
        map.put("articleCoverUrl", productArticle.getArticlecoverurl() == null ? "" : productArticle.getArticlecoverurl());
        map.put("articleHistoryDate", productArticle.getArticlehistorydate() == null ? "" : productArticle.getArticlehistorydate());
        map.put("createTime", productArticle.getCreatetime() == null ? "" : productArticle.getCreatetime());
        redisTemplate.opsForValue().set(productArticle.getCatalogid() + "_" + productArticle.getArticlehistorymonthday(), JSON.toJSONString(map));
    }

    public void deleteStickypostsRedis(ProductArticleDO productArticle) {
        redisTemplate.delete(productArticle.getCatalogid() + "_" + productArticle.getArticlehistorymonthday());
    }
}
